<template>
  <a-modal :title="title" :width="800" :visible="visible" :footer="null" @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <div id="printContent" class="print-content">
        <div class="print-header">
          <h1>出库单</h1>
          <div class="print-no">单号: {{ outbound.outboundNo }}</div>
        </div>

        <div class="print-info">
          <div class="print-info-item">
            <span class="label">出库类型:</span>
            <span class="value">{{ getOutboundTypeText(outbound.outboundType) }}</span>
          </div>
          <div class="print-info-item">
            <span class="label">出库日期:</span>
            <span class="value">{{ outbound.outboundDate }}</span>
          </div>
          <div class="print-info-item">
            <span class="label">状态:</span>
            <span class="value">{{ getStatusText(outbound.status) }}</span>
          </div>
          <div class="print-info-item" v-if="outbound.orderNo">
            <span class="label">关联订单号:</span>
            <span class="value">{{ outbound.orderNo }}</span>
          </div>
          <div class="print-info-item" v-if="outbound.requisitionNo">
            <span class="label">关联领料单号:</span>
            <span class="value">{{ outbound.requisitionNo }}</span>
          </div>
        </div>

        <div class="print-table">
          <table>
            <thead>
              <tr>
                <th>序号</th>
                <th>物料类型</th>
                <th>物料编码</th>
                <th>物料名称</th>
                <th>系列</th>
                <th>颜色</th>
                <th>数量</th>
                <th>单位</th>
                <th>计划入库仓库/库位</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in outbound.outboundDetailList" :key="index">
                <td>{{ index + 1 }}</td>
                <td>{{ getMaterialTypeText(item.materialType) }}</td>
                <td>{{ item.materialCode }}</td>
                <td>{{ item.materialName }}</td>
                <td>{{ item.materialSeries }}</td>
                <td>{{ item.color }}</td>
                <td>{{ item.quantity }}</td>
                <td>{{ item.unit }}</td>
                <td>{{ item.warehouseName }}/{{ item.locationName }}</td>
              </tr>
            </tbody>
          </table>
        </div>

        <div class="print-remark" v-if="outbound.remark">
          <div class="label">备注:</div>
          <div class="value">{{ outbound.remark }}</div>
        </div>

        <div class="print-footer">
          <div class="print-sign">
            <div class="sign-item">
              <div class="label">领料人:</div>
              <div class="line"></div>
            </div>
            <div class="sign-item">
              <div class="label">仓库管理员:</div>
              <div class="line"></div>
            </div>
            <div class="sign-item">
              <div class="label">审核人:</div>
              <div class="line"></div>
            </div>
          </div>
          <div class="print-date">打印日期: {{ printDate }}</div>
        </div>
      </div>

      <div class="print-actions">
        <a-button type="primary" @click="handlePrint"> <a-icon type="printer" />打印 </a-button>
        <a-button @click="handleCancel" style="margin-left: 8px"> 关闭 </a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { getOutboundById } from '@/api/wms/outbound'
import moment from 'moment'
import { ajaxGetDictItems } from '@/api/api'

export default {
  name: 'OutboundPrint',
  data() {
    return {
      title: '打印',
      visible: false,
      confirmLoading: false,
      outbound: {},
      printDate: moment().format('YYYY-MM-DD HH:mm:ss'),
      outboundTypeDict: [],
      statusDict: [],
      materialTypeDict: [],
    }
  },
  created() {
    this.loadDictData()
  },
  methods: {
    loadDictData() {
      // 加载字典数据
      ajaxGetDictItems('wms_outbound_type').then((res) => {
        if (res.success) {
          this.outboundTypeDict = res.result
        }
      })
      ajaxGetDictItems('wms_outbound_status').then((res) => {
        if (res.success) {
          this.statusDict = res.result
        }
      })
      ajaxGetDictItems('material_type').then((res) => {
        if (res.success) {
          this.materialTypeDict = res.result
        }
      })
    },
    show(record) {
      this.visible = true
      this.outbound = {}
      this.loadDetail(record.id)
    },
    loadDetail(id) {
      this.confirmLoading = true
      getOutboundById({ id: id })
        .then((res) => {
          if (res.success) {
            this.outbound = res.result
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
    handlePrint() {
      // 创建新窗口进行打印，避免影响当前页面
      const printWindow = window.open('', '_blank', 'width=800,height=600')
      const printContent = document.getElementById('printContent').innerHTML

      printWindow.document.write(`
          <!DOCTYPE html>
          <html>
          <head>
            <title>出库单打印</title>
            <style>
              body { margin: 0; padding: 20px; font-family: SimSun, sans-serif; }
              .print-content { width: 100%; max-width: 800px; margin: 0 auto; }
              .print-header { text-align: center; margin-bottom: 20px; }
              .print-header h1 { font-size: 24px; margin: 0; }
              .print-no { font-size: 14px; margin-top: 10px; }
              .print-info { display: flex; flex-wrap: wrap; margin-bottom: 20px; }
              .print-info-item { width: 50%; margin-bottom: 10px; display: flex; }
              .label { font-weight: bold; margin-right: 10px; min-width: 80px; }
              .print-table { margin-bottom: 20px; }
              table { width: 100%; border-collapse: collapse; }
              th, td { border: 1px solid #000; padding: 8px; text-align: center; font-size: 12px; }
              th { background-color: #f0f0f0; }
              .print-remark { margin-bottom: 20px; }
              .print-footer { margin-top: 40px; }
              .print-sign { display: flex; justify-content: space-between; margin-bottom: 40px; }
              .sign-item { width: 30%; }
              .line { border-bottom: 1px solid #000; height: 30px; }
              .print-date { text-align: right; font-size: 12px; }
              @media print {
                @page { margin: 0.5in; }
                body { margin: 0; }
              }
            </style>
          </head>
          <body>
            ${printContent}
          </body>
          </html>
        `)

      printWindow.document.close()

      // 等待内容加载完成后打印
      printWindow.onload = function () {
        printWindow.print()
        printWindow.close()
      }
    },
    getOutboundTypeText(value) {
      const item = this.outboundTypeDict.find((item) => item.value === value)
      return item ? item.text : value
    },
    getStatusText(value) {
      const item = this.statusDict.find((item) => item.value === value)
      return item ? item.text : value
    },
    getMaterialTypeText(value) {
      const item = this.materialTypeDict.find((item) => item.value === value)
      return item ? item.text : value
    },
  },
}
</script>

<style scoped>
.print-content {
  padding: 20px;
  background-color: #fff;
}
.print-header {
  text-align: center;
  margin-bottom: 20px;
}
.print-header h1 {
  font-size: 24px;
  margin: 0;
}
.print-no {
  font-size: 14px;
  margin-top: 10px;
}
.print-info {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.print-info-item {
  width: 50%;
  margin-bottom: 10px;
  display: flex;
}
.label {
  font-weight: bold;
  margin-right: 10px;
  min-width: 80px;
}
.print-table {
  margin-bottom: 20px;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  border: 1px solid #000;
  padding: 8px;
  text-align: center;
}
th {
  background-color: #f0f0f0;
}
.print-remark {
  margin-bottom: 20px;
}
.print-footer {
  margin-top: 40px;
}
.print-sign {
  display: flex;
  justify-content: space-between;
  margin-bottom: 40px;
}
.sign-item {
  width: 30%;
}
.line {
  border-bottom: 1px solid #000;
  height: 30px;
}
.print-date {
  text-align: right;
  font-size: 12px;
}
.print-actions {
  text-align: center;
  margin-top: 20px;
}
</style> 